from kafka import KafkaProducer
import random
import time
import json
import md5
producer = KafkaProducer(bootstrap_servers='10.120.241.82:9092')

arr = ['GjsWebLiveRoomMsg', 'UserOptionalMsg', 'BackPayMsg', 'GoldInOutMoneyMsg']
var = 1
gisWebLiveRoomMsg = { "startTime": "1484284715", "roomId": "29", "messageSentTime": 1484027026.505, "userIP": "171.83.104.167", "pcWapType": "app_wap", "eventTime": "1484027025", "serverIP": "114.113.198.179", "eventName": "heartbeat", "videoRunning": "1", "userId": "m17386033462@163.com", "pbtype": "GjsWebLiveRoomMsg" }

userOptionalMsg = { "serverIP": "114.113.198.179", "messageSentTime": "1484027067.955", "userIP": "223.73.144.83", "sessionId": "6224765b4ede6f9c0e0dd70c4597862d", "appKey": "cp_netease_ios", "eventTime": "1484027067545", "deviceId": "A444BE97-998C-4EC0-8CF3-79ADB595CD56", "eventName": "LiveRoomDetailEvent", "item": [ { "key": "userName", "value": "uglymoney@163.com" },{ "key": "allowed", "value": 0 },{ "key": "startTime", "value": "1484284715" },{ "key": "roomId", "value": "25" } ], "pbtype": "nldmessage.UserOptionalMsg" }

backPayMsg = {"accountId":"921A93B4C0D99AF41BF657F19A0CAF0C","clientType":"ANDROID","eventName":"back_order_pay_success","eventTime":"1484223226791","orderId":"2017011220HY023248521","payAmount":"7.0","payMethod":"xx","payTime":"1484223226791","product":"yiyuanlegou","serverIp":"10.120.241.221","userIp":"220.181.102.176"}

goldInOutMoneyMsg = {"accountId":"AF3DC7F490A6C6958DD80774A76DE610","bankCardNo":"6222620110031408850","clientType":"APP","createTime":"1483935467607","currencyType":"00","eventName":"gjs_in_out_money","eventTime":"1483935470931","failReason":"","firmId":"100636283","localOrderId":"00000000000111828400","notifyStatus":"1","orderId":"ODR201701096326403894","orderMoney":"422.41","orderMoneyCent":"42241","orderType":"withdraw","orderTypeDisplay":"0","partnerId":"pmec","product":"gold","realTradeMoney":"422.41","realTradeMoneyCent":"42241","realTradeTime":"1483935470931","reconStatus":"0","serverIp":"","status":"2","updateTime":"1483935467000","userIp":""}


def msg(n):
    if n == 0:
        gisWebLiveRoomMsg["eventTime"] = str(int(time.time()))
        gisWebLiveRoomMsg["userId"] = str(random.randint(0, 5))
        return json.dumps(gisWebLiveRoomMsg)
    if n == 1:
        userOptionalMsg["eventTime"] = str(int(time.time()) * 1000)
        userOptionalMsg["item"][0]["value"] = str(random.randint(0, 5))
        return json.dumps(userOptionalMsg)
    if n == 2:
        backPayMsg["eventTime"] = str(int(time.time()) * 1000)
        backPayMsg["payTime"] = str(int(time.time()) * 1000)
        backPayMsg["payAmount"] = str(random.randint(0, 1000))
        m1 = md5.new()
        m1.update(str(random.randint(0, 5)))
        backPayMsg["accountId"] = m1.hexdigest().upper()
        return json.dumps(backPayMsg)
    if n == 3:
        goldInOutMoneyMsg["eventTime"] = str(int(time.time()) * 1000)
        goldInOutMoneyMsg["realTradeTime"] = str(int(time.time()) * 1000)
        goldInOutMoneyMsg["orderMoney"] = str(random.randint(0, 1000))
        m1 = md5.new()
        m1.update(str(random.randint(0, 5)))
        goldInOutMoneyMsg["accountId"] = m1.hexdigest().upper()
        return json.dumps(goldInOutMoneyMsg)


var = 1
while var == 1:
    n = random.randint(0, 3)
    producer.send(arr[n],msg(n))
    producer.flush()
    time.sleep(2)
producer.close()

